<?php

/**
 * RestaurantMsgtpl订餐发送消息
 * @author hjc19850430@gmail.com
 */
class BookMsgtpl extends BaseMsgtpl
{

	/**
	 * 发送消息给用户
	 * @param string $orderInfo
	 * @param string $accountId
	 */
	public function sendMessage($orderInfo,$accountId)
	{
        error_log('xiaoxi-1'.var_export($accountId,true)."\r\n",3,'/tmp/xiaoxi.log');
		//查询订单信息
		$orderGoodsInfo = D('Base/BookOrderGoods')->getList(array('order_id' => $orderInfo['order_id']), array('goods_name','goods_num'));
        $info = D('Base/BookInfo')->getByPk($orderInfo['restaurant_id']);

		$condition = array(
			'user_id' => $orderInfo['user_id'],
		);

		//查询微信用户信息
		$wechatUser = D('Wechat/WechatUser')->getInfo($condition,'openid,subscribe');
		$url = C('site_url')."/base/book/orderDetail/res_id/".$orderInfo['res_id']."/order_id/".$orderInfo['order_id'];

		$str = '';

		$name = '';
		$num = '';
		if($orderGoodsInfo) {
			foreach($orderGoodsInfo as $key => $v) {
				$str = $str . $v['goods_name'] . '*' . $v['goods_num'] . '  ';

                $name = $v['goods_name'];
				$num = $v['goods_num'];
			}
		}

		//查询餐厅消息设置
		$messageInfo = D('Base/BookMessage')->getInfo(array('restaurant_id' => $orderInfo['restaurant_id'], 'message_type' => 1));
		if($wechatUser['subscribe']) {
			if($accountId == 1){
				$this->wxTplSign = 'BypiRvSt1PW5vTRbvukx0kYtPrh7_naMEFwqhhmgU4w';
			}elseif($accountId == 2) { //滨江后勤
				$this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
			} elseif($accountId == 5) {  //农林
				$this->wxTplSign = 'En2hbZ6GDZsh3DR_DaQWlFsr98HUDU9kLBkolfcVN-A';
			} elseif($accountId == 6) {  //暨阳学院
				$this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
			}elseif($accountId == 25){ // 订餐
				$this->wxTplSign = 'ahcFELk4XN7HvaT1GqEqsuwhhEgwFAPtjl5UNxy7Tg8';
			}elseif($accountId == 60){
				$this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
			}elseif($accountId == 47){
				$this->wxTplSign = 'J2BJEi-DHSPXv8DHCE-TvlFapGgn36U2w6seRdPxw-g';
            }elseif($accountId == 59){
                $this->wxTplSign = 'leXVdtkAYZPHwrHAt-k1hlmHZUe9k1KrQxzt6d0Wg2M';
            }

			if($accountId == 1){
				$info = array(
					'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '下单成功，我们会尽快发货！！'),
					'keyword1' => array("value" => '餐厅订餐'),
					'keyword2' => array("value" => date("Y-m-d H:i:s")),
					'keyword3' => array("value" => $str),
				);
			}elseif($accountId == 47){//47
				$info = array(
					'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '下单成功，我们会尽快发货！！'),
					'keyword1' => array("value" => $orderInfo['order_sn']),
					'keyword2' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '下单成功，我们会尽快发货！！'),
					'keyword3' => array("value" => "订单下单成功"),
					'keyword4' => array("value" => date("Y-m-d H:i:s")),
					'remark'   => array("value" => "查看详情"),
				);
			}elseif($accountId == 60){
                $str = '';
                if($orderGoodsInfo) {
                    foreach($orderGoodsInfo as $key => $v) {
                        $str .="【已下厨】       ".$v['goods_name'] . "   " . $v['goods_num'] . "份 \n";
                    }
                }
                $str .= "....\n";
                $str .= '点击这里查看菜肴状态并及时取餐';
				$info = array(
					'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '您于'.date("Y年m月d日 H:i",$orderInfo['create_time']).'的订单，菜肴已下厨，请耐心等待'),
					'keyword1' => array("value" => $info['restaurant_name']),
                    'keyword2' => array("value" => ""),
                    'remark' => array("value" => $str),
				);
                $url = C('site_url')."/book/ZduMobile/orderDetail/res_id/".$orderInfo['res_id']."/order_id/".$orderInfo['order_id'];

            }elseif($accountId == 59){
                $str = '';
                if($orderGoodsInfo) {
                    foreach($orderGoodsInfo as $key => $v) {
                        $str .="【已下厨】       ".$v['goods_name'] . "   " . $v['goods_num'] . "份 \n";
                    }
                }
                $str .= "....\n";
                $str .= '点击这里查看菜肴状态并及时取餐';

                $info = array(
                    'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '您于'.date("Y年m月d日 H:i",$orderInfo['create_time']).'的订单，菜肴已下厨，请耐心等待'),
                    'keyword1' => array("value" => $info['restaurant_name']),
                    'keyword2' => array("value" => $info['mobile']),
                    'keyword3' => array("value" => $orderInfo['order_num'],"color" => "#ff0000"),
                    'keyword4' => array("value" => $orderInfo['order_sn']),
                    'remark' => array("value" => $str),
                );
                $url = C('site_url')."/book/ZduMobile/orderDetail/res_id/".$orderInfo['res_id']."/order_id/".$orderInfo['order_id'];

            }else{
                $info = array(
                    'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '下单成功，我们会尽快发货！！'),
                    'keyword1' => array("value" => '餐厅订餐'),
                    'keyword2' => array("value" => date("Y-m-d H:i:s")),
                );
            }
            error_log('xiaoxi-2'.var_export($this->wxTplSign,true)."\r\n",3,'/tmp/xiaoxi.log');
            error_log('xiaoxi-3'.var_export($info,true)."\r\n",3,'/tmp/xiaoxi.log');
			$this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
		}
		return true;
	}

	/**
	*发货给学生推消息
	 */
	public function sendInfoToUser($orderInfo,$accountId){
		$url = C('site_url')."/base/book/orderDetail/res_id/".$orderInfo['res_id']."/order_id/".$orderInfo['order_id'];

		$wechatUser = D('Wechat/WechatUser')->getInfo(array('user_id' => $orderInfo['user_id']), 'openid,subscribe');
		//查询订单信息
		$orderGoodsInfo = D('Base/BookOrderGoods')->getList(array('order_id' => $orderInfo['order_id']),array('goods_name','goods_num'));

		$str = '';
		if ($orderGoodsInfo) {
			foreach ($orderGoodsInfo as $key => $v) {
				$str = $str . $v['goods_name'] . '*' . $v['goods_num'] . '  ';
			}
		}

		//查询餐厅消息设置
		$messageInfo = D('Base/BookMessage')->getInfo(array('restaurant_id' => $orderInfo['restaurant_id'], 'message_type' => 2));

		if ($wechatUser['subscribe']) {
			if($accountId == 2) { //滨江后勤
				$this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
			} elseif($accountId == 5) {  //农林
				$this->wxTplSign = 'En2hbZ6GDZsh3DR_DaQWlFsr98HUDU9kLBkolfcVN-A';
			} elseif($accountId == 6) {  //暨阳学院
				$this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
			} elseif($accountId == 25){ // 订餐
				$this->wxTplSign = 'ahcFELk4XN7HvaT1GqEqsuwhhEgwFAPtjl5UNxy7Tg8';
			}elseif($accountId == 60){
				$this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
			}elseif($accountId == 47){
				$this->wxTplSign = 'J2BJEi-DHSPXv8DHCE-TvlFapGgn36U2w6seRdPxw-g';
			}
			if($accountId == 47){
				$info = array(
					'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '您的美味已经发货！！！'),
					'keyword1' => array("value" => $orderInfo['order_sn']),
					'keyword2' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '您的美味已经发货！！！'),
					'keyword3' => array("value" => "订单下单成功"),
					'keyword4' => array("value" => date("Y-m-d H:i:s")),
					'remark'   => array("value" => "查看详情"),
				);
			}elseif($accountId == 60){
				$info = array(
					'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '您的美味已经在路上了！!'),
					'keyword1' => array("value" => '餐厅订餐'),
					'keyword2' => array("value" => date("Y-m-d H:i:s")),
					'keyword3' => array("value" => $str),
				);
                $url = C('site_url')."/book/ZduMobile/orderDetail/res_id/".$orderInfo['res_id']."/order_id/".$orderInfo['order_id'];

            }else{
                $info = array(
                    'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '您的美味已经在路上了！!'),
                    'keyword1' => array("value" => '餐厅订餐'),
                    'keyword2' => array("value" => date("Y-m-d H:i:s")),
                    'keyword3' => array("value" => $str),
                );
            }

			$this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
		}
	}


	/**
	 * 发送消息给餐厅管理员
	 * @param string $orderInfo 	订单信息
	 * @param string $accountId 	账号id
	 * @param string $type 	1：用户申请退款
	 */
	public function sendMessageToManage($orderInfo,$accountId,$type='')
	{
        error_log('xiaoxi-4'.var_export($accountId,true)."\r\n",3,'/tmp/xiaoxi.log');
        error_log('xiaoxi-5'.var_export($type,true)."\r\n",3,'/tmp/xiaoxi.log');
		$managerUser = D('Base/BookManager')->getList(array('restaurant_id' => $orderInfo['restaurant_id'], 'cut_off' => 0),array('wechat_user_id'));
		if($managerUser) {
			foreach($managerUser as $val) {
				$wechatUser = D('Wechat/WechatUser')->getInfo(array('wechat_user_id' => $val['wechat_user_id']),'openid,subscribe');
				$url = C('site_url')."/base/book/sendList/res_id/" . $orderInfo['res_id'];

				//查询订单信息
				$orderGoodsInfo = D('Base/BookOrderGoods')->getList(array('order_id' => $orderInfo['order_id']),array('goods_name','goods_num'));

				$str = '';
				$name = '';
				$num = '';
				if($orderGoodsInfo) {
					foreach($orderGoodsInfo as $key => $v) {
						$str = $str . $v['goods_name'] . '*' . $v['goods_num'] . '  ';
						$name = $v['goods_name'];
						$num = $v['goods_num'];
					}
				}

				//查询餐厅消息设置
				$messageInfo = D('Base/BookMessage')->getInfo(array('restaurant_id' => $orderInfo['restaurant_id'], 'message_type' => 3));

				if($wechatUser['subscribe']) {

                    if($accountId == 2) { //滨江后勤
                        $this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
                    } elseif($accountId == 5) {  //农林
                        $this->wxTplSign = 'En2hbZ6GDZsh3DR_DaQWlFsr98HUDU9kLBkolfcVN-A';
                    } elseif($accountId == 6) {  //暨阳学院
                        $this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
                    } elseif($accountId == 25){ // 订餐
                        $this->wxTplSign = 'ahcFELk4XN7HvaT1GqEqsuwhhEgwFAPtjl5UNxy7Tg8';
                    }elseif($accountId == 60){
                        $this->wxTplSign = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk';
                    }elseif($accountId == 61){
                        $this->wxTplSign = 'aEqj3u_EPKI9BzmHXKwWmGm1sdJEqbQ_j4rbZe7qEUk';
                    }elseif($accountId == 47){
                        if($type == 1){
                            $this->wxTplSign = 'SNFBcuVLAGZhSUaKNVbnay5xsjgtybE-jHeeAqtSUlw';
                        }else{
                            $this->wxTplSign = 'J2BJEi-DHSPXv8DHCE-TvlFapGgn36U2w6seRdPxw-g';
                        }
                    }
				if($accountId == 47){
                    if($type == 1){
                        $info = array(
                            'first' => array("value" => '用户已取消订单，请审核！'),
                            'keyword1' => array("value" => $orderInfo['user_name']),
                            'keyword2' => array("value" => date("Y-m-d H:i:s")),
                            'keyword3' => array("value" => $orderInfo['order_sn']),
                            'keyword4' => array("value" => $orderInfo['refund_reason']),
                            'remark'   => array("value" => "查看详情"),
                        );
                    }else{
                        $info = array(
                            'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '生意来了，火速处理！！'),
                            'keyword1' => array("value" => $orderInfo['order_sn']),
                            'keyword2' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '生意来了，火速处理！！'),
                            'keyword3' => array("value" => "订单下单成功"),
                            'keyword4' => array("value" => date("Y-m-d H:i:s")),
                            'remark'   => array("value" => "查看详情"),
                        );
                    }

				}elseif(($accountId == 60)){
                    if($type == 1){
                        $info = array(
                            'first' => array("value" =>'用户已取消订单,请审核！'),
                            'keyword1' => array("value" => '餐厅订餐'),
                            'keyword2' => array("value" => date("Y-m-d H:i:s")),
                        );
                    }else{
                        $info = array(
                            'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '生意来了，火速处理！！'),
                            'keyword1' => array("value" => '餐厅订餐'),
                            'keyword2' => array("value" => date("Y-m-d H:i:s")),
                        );
                    }
				}elseif($accountId == 61){
				    if($type == 1){
                        $info = array(
                            'name' => array("value" =>"用户已取消订单，请及时审核！"),
                            'remark' => array("value" => '​申请退款，请及时处理！'),
                        );
                    }else{
                        $info = array(
                            'name' => array("value" =>"用户购买成功！"),
                            'remark' => array("value" => '​请及时发货！'),
                        );
                    }

                } else{
					$info = array(
						'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '生意来了，火速处理！！'),
						'keyword1' => array("value" => '餐厅订餐'),
						'keyword2' => array("value" => date("Y-m-d H:i:s")),
						'keyword3' => array("value" => $str),
					);
				}
                $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
				}
			}
		}
	}

}
